import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_book/api/search/search_api.dart';
import 'package:flutter_book/constant/color.dart';
import 'package:flutter_book/constant/style.dart';
import 'package:flutter_book/data/model/book_model.dart';
import 'package:flutter_book/route/router.dart';
import 'package:flutter_book/util/extensions/string_extensions.dart';
import 'package:flutter_book/widget/common/Image_widget.dart';
import 'package:flutter_book/widget/common/smart_refressher_head_footer.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter_book/util/extensions/widget_extensions.dart';

class SearchResultPage extends StatefulWidget {
  final String searchText;

  SearchResultPage(this.searchText);

  @override
  _SearchResultPageState createState() => _SearchResultPageState();
}

class _SearchResultPageState extends State<SearchResultPage> {
  final RefreshController _refreshController = RefreshController(initialRefresh: true);
  final ScrollController _scrollController = ScrollController();
  final List<BookModel> bookList = [];

  @override
  Widget build(BuildContext context) {
    return Container(
      color: AppColor.bgWhite,
      child: SmartRefresher(
          enablePullUp: false,
          enablePullDown: true,
          footer: SmartRefresherHeadFooter.init().getFooter(),
          header: SmartRefresherHeadFooter.init().getHeader(),
          controller: _refreshController,
          onLoading: _onLoading,
          onRefresh: _onRefresh,
          child: ListView.builder(
              controller: _scrollController,
              itemCount: bookList.length,
              itemBuilder: (context, index) {
                return _getItem(bookList[index]);
              })),
    );
  }

  Widget _getItem(BookModel bookModel) {
    return Container(
      width: Get.width,
      color: AppColor.transparent,
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      child: Row(
        children: [
          Container(
            child: ImageWidget.network(bookModel.cover, width: 79, height: 119, radius: 4),
          ),
          SizedBox(width: 12),
          Expanded(
            child: Container(
              alignment: Alignment.topCenter,
              height: 119.0,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(height: 1),
                  Text(bookModel.title ?? "", style: AppStyle.textMedium18, textAlign: TextAlign.start),
                  SizedBox(height: 6),
                  Text(bookModel.author ?? "", style: AppStyle.textRegular16, textAlign: TextAlign.start),
                  SizedBox(height: 6),
                  Expanded(
                    child: Container(
                      child: SingleChildScrollView(
                        physics: NeverScrollableScrollPhysics(),
                        child: Text(
                          bookModel.shortIntro ?? "",
                          style: AppStyle.textHintRegular13,
                          overflow: TextOverflow.ellipsis,
                          maxLines: 100,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    ).onTap(() {
      AppRouter.toBookDetailPage(bookModel);
    });
  }

  _onLoading() {
    "加载更多".print;
  }

  _onRefresh() async {
    "刷新界面".print;
    bookList.clear();
    bookList.addAll(await SearchApi.init().searchTargetKeyWord(widget.searchText));
    _refreshController.refreshCompleted();
    setState(() {});
  }
}
